<template>
  <div class="app-container">
    <!--查询表单开始-->
    <el-form :inline="true" class="demo-form-inline">

      <el-form-item>
        <el-input v-model="courseQuery.title" placeholder="课程名称"/>
      </el-form-item>

      <el-form-item label="价格区间">
        <el-input-number v-model="courseQuery.minPrice" :min="0"  placeholder="最低价" value="0"/>
      </el-form-item>
      <el-form-item>
        <el-input-number v-model="courseQuery.maxPrice" :min="0"  placeholder="最高价" value="0"/>
      </el-form-item>
      <el-form-item label="添加时间">
        <el-date-picker v-model="courseQuery.begin" type="datetime" placeholder="选择开始时间"
                        value-format="yyyy-MM-dd HH:mm:ss" default-time="00:00:00"/>
      </el-form-item>
      <el-form-item>
        <el-date-picker v-model="courseQuery.end" type="datetime" placeholder="选择截止时间"
                        value-format="yyyy-MM-dd HH:mm:ss" default-time="00:00:00"/>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
      <el-button type="default" @click="resetData()">清空</el-button>
    </el-form>
    <!--查询表单结束-->
    <!-- 表格开始 -->
    <!--  v-loading="listLoading"   -->
    <el-table :data="list" element-loading-text="数据加载中" border fit highlight-current-row>
      <el-table-column label="序号" width="70" align="center">
        <template slot-scope="scope">{{ (page - 1) * limit + scope.$index + 1 }}</template>
      </el-table-column>
      <el-table-column prop="title" label="课程名称" width="180"/>
      <!--      <el-table-column prop="teacherId" label="讲师" width="80"/>-->
      <!--      <el-table-column prop="subjectId" label="分类" width="80"/>-->
      <el-table-column prop="price" label="价格" width="80"/>
      <el-table-column prop="buyCount" label="销售量" width="80"/>
      <el-table-column prop="viewCount" label="浏览量" width="80"/>

      <el-table-column label="课程状态" width="80">
        <template slot-scope="scope">{{ scope.row.status === 'Normal' ? '已发布' : '未发布' }}</template>
      </el-table-column>
      <el-table-column prop="gmtCreate" label="添加时间" width="160"/>
      <el-table-column label="操作" width="350" align="center">
        <template slot-scope="scope">
          <router-link :to="'/teacher/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">基本信息</el-button>
          </router-link>
          <router-link :to="'/teacher/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">大纲</el-button>
          </router-link>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeCourse(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表格结束 -->
    <!-- 分页 -->
    <el-pagination :current-page="page" :page-size="limit" :total="total" style="padding: 30px 0; text-align: center;"
                   layout="total, prev, pager, next, jumper" @current-change="fetchData"
    />
  </div>
</template>

<script>
import {getCourseListPage, removeCourse} from "@/api/edu/course";

export default {
  name: "index",
  data() {
    return {
      page: 1,//当前页
      limit: 5,//每页显示数据条数
      courseQuery: {},//条件封装的对象
      list: null,//查询之后返回的数据集合
      total: 0,//总记录数
    }
  },
  created() {
    this.getCourseListPage();
  },
  methods: {
    //获取讲师集合
    getCourseListPage(page = 1) {
      getCourseListPage(page, this.limit, this.courseQuery).then(res => {
        this.list = res.data.records;
        this.total = res.data.total;
      }).catch(err => {
        console.log(err)
      });
    },
    //切换页码,多条件分页查询
    fetchData(page = 1) {
      this.page = page;
      this.getCourseListPage(page);
    },
    //清空查询对象
    resetData() {
      this.courseQuery = {};
      this.getCourseListPage();
    },
    //删除课程信息
    removeCourse(id) {
      // console.log(memberId)
      this.$confirm('此操作将永久删除课程信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return removeCourse(id);
      }).then(() => {
        //如果表格当前页剩余最后一条，并且当前页不为1，则当前页面-1
        if (this.list.length === 1 && this.page !== 1) {
          this.page -= 1;
        }
        this.fetchData(this.page);
        //提示信息
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch((response) => { // 失败
        if (response !== 'cancel') {
          this.$message({
            type: 'error',
            message: '删除失败'
          })
        }
      })

    }
  }

}
</script>

<style scoped>

</style>
